package tags;

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;


/**
 * This class is a custom action for writing the value of a
 * cookie to the response.
 *
 * @author Hans Bergsten, Gefion software <hans@gefionsoftware.com>
 * @version 1.0
 */
public class GetCookieValueTag extends TagSupport {
    /**
	 * 
	 */
	private static final long serialVersionUID = -4304048864134109959L;
	private String name;

    /**
     * Sets the cookie name attribute.
     *
     * @param name the name of the cookie
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Writes the specified cookie value to the output stream,
     * or an empty string if not found.
     */
    @Override
	public int doEndTag() throws JspException {
        try{
	    	String value = CookieUtils.getCookieValue(name,(HttpServletRequest) pageContext.getRequest());
	        if (value == null) {
	            value = "";
	        }
	        try {
	            pageContext.getOut().write(value);
	        }
	        catch (IOException e) {} // Ignore it
        }catch (Exception e) {} // On ignore le fait qu'il ne trouve pas de cookie
        return EVAL_PAGE;
    }
    
    /**
     * Releases all instance variables.
     */
    @Override
	public void release() {
        name = null;
        super.release();
    }
}
